
<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>


<div id="subcontent">
<h4>Add Opponent Info</h4>


<form>
	
<select class="form-control"  name="season" id="select3">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM seasons";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

<?php

if (isset($_POST['season'])) {
  
  $setseason = $_POST['season'];

?>
<br>
<select class="form-control" name="team" id="select2">

<?php

if (isset($_POST['team'])) {

	$team = $_POST['team'];

	echo "<option value=\"$team\" selected=\"selected\">$team is selected</option>";


} else {         	
	echo "<option value=\"\" disabled=\"disabled\" selected=\"selected\">Select a team</option>";
}


  $sql2 = "SELECT teamname
  FROM opponents
  WHERE season = '$setseason'";


  $result2=$db->query($sql2);

  while($row2=$result2->fetch_assoc()){	

    $team=$row2['teamname']; 
  
  echo "<option value=\"$team\">$team</option>";

}

?>

 </select>

 <?php } 

if (isset($_POST['team']) && isset($_POST['season'])) {
	
?>
<br>
<textarea class="form-control" name="text" placeholder="Opponent information" rows="10"></textarea>


<br>

<button class="form-control btn btn-success" id="submit" type="submit">Add</button>

<? }; ?>

</form>

<br>




</div>


<?php } ?>

<script type="text/javascript">
	
$("#select3").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./opponents_add.php",
        data: postData,
        success: function(data) { 
            $("#subcontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#subcontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#select2").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./opponents_add.php",
        data: postData,
        success: function(data) { 
            $("#subcontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#subcontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#submit").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./includes/handlers/opponents_add_handler.php",
        data: postData,
        success: function(data) { 
            $("#subcontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#subcontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>